export default{
  // 开启命名空间
  namespaced:true,
  
  // 数据节点
  state:() => ({
    // 储存收藏
    collection:JSON.parse(uni.getStorageSync('collection') || '[]'),
    
    // 用于判定物品是否收藏的参数
    itemIsCollected:true,
    // JSON.parse(uni.getStorageSync('itemIsCollected') || ('[]'))
    

  }),
  
  //方法
  
  mutations:{
    //点击收藏处理函数
    collectClickHandler(state,goods){
      const findResult = state.collection.find(x => x.goods_id === goods.goods_id)
      
      // 找不到则添加
      if(!findResult){
        // 将属性值变为已收藏然后加进数组
        goods.goods_state_collected = true

        state.collection.push(goods)
        // this.commit('m_collection/changeItemIsCollected',true)
        state.itemIsCollected = true
        
        // 储存
        this.commit('m_collection/saveToStorage')
        
        // 把收藏的id给后端，不用给整个对象
        
        uni.$showMsg('收藏成功!')
        
      }
      
      else{
        state.collection = state.collection.filter(x => x.goods_id !== goods.goods_id)
        
        // this.commit('m_collection/changeItemIsCollected',false)
        state.itemIsCollected = false
        
        this.commit('m_collection/saveToStorage')
        
        // 把取消收藏的id给后端，不用给整个对象
        
        uni.$showMsg('取消收藏成功!')
      }
      

    },
    
    // 更新收藏状态
    updateCollectionState(state,goods){
      const findResult = state.collection.find(x => x.goods_id === goods.goods_id)
      
      // 如果找到
      if(findResult){
        findResult.goods_state_collected = goods.goods_state_collected
        this.commit('m_collection/saveToStorage')
      }
    },
    
    // 储存收藏数据
    saveToStorage(state){
      uni.setStorageSync('collection',JSON.stringify(state.collection))
      uni.setStorageSync('itemIsCollected',JSON.stringify(state.itemIsCollected))
    },
    

    
    // 删除商品
    removeGoodsById(state,goods_id){
        state.collection = state.collection.filter(x => x.goods_id !== goods_id)
        this.commit('m_collection/saveToStorage')
    },
    
    // 查询某个商品是否被收藏
    // isCollected(state,goods){
    //   const findResult = state.collection.find(x => x.goods_id === goods.goods_id)
      
    //   // console.log(findResult)
      
    //   // 如果找到
    //   if(findResult){
    //     console.log("true")
    //     state.itemIsCollected = true
    //   }else{
    //     console.log("false")
    //     state.itemIsCollected = false
    //   }
      
    // },
    
    // 用于设定itemIsCollected的值
    changeValue(state,newVal){
      state.itemIsCollected = newVal
      this.commit('m_collection/saveToStorage')
    },
    
    // 获取从服务端发来的收藏
    updateAllCollection(state,collectionList){
      state.collection = collectionList
      this.commit('m_collection/saveToStorage')
    },
    
    // 判断本地储存的收藏中是否有该物品
    isCollected(state,goods_id){
      const findResult = state.collection.find(x => x.goods_id == goods_id)

      // console.log(typeof goods_id)
      // console.log('123123')
      // // console.log('123456')
      // console.log(findResult)
      
      if(findResult){
        state.itemIsCollected = true
        
         this.commit('m_collection/saveToStorage')
        // this.commit('m_collection/changeItemIsCollected',true)
      }else{
        state.itemIsCollected = false
        // console.log('abc')
         this.commit('m_collection/saveToStorage')
        // this.commit('m_collection/changeItemIsCollected',false)
      }
    },
    
    // 更改itemIsCollected的值
    changeItemIsCollected(state,newVal){
      uni.setStorageSync('itemIsCollected',JSON.stringify(newVal))
    }
    

  },
  
  // 获取对象
  getters:{
    
    // 统计收藏物品总数
    totalCollection(state){
      return state.collection.reduce((total,item) => {
        return total += 1
      },0)
    }
  }
}